﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
namespace Kunlun.BusinessLogic
{
    public class PhotoChangeSize
    {
        private string _oldfile;
        private string _newfile;
        private int _width;
        private int _height;
        public string OldFile { get{return this._oldfile;} set{this._oldfile=value;} }
        public string NewFile { get { return this._newfile; } set { this._newfile = value; } }
        public int Width { get { return this._width; } }
        public int Height { get { return this._height; } }
        public void ChangSize(int width)// m="m" or m="s"
        {        
            
            try
            {


                int height;
                Image oFile = System.Drawing.Image.FromFile(OldFile);
                _height = oFile.Height;
                _width = oFile.Width;
                if (width != 0 && (_width > _height || _width == _height))
                {
                    if (width > oFile.Width)
                    {
                        width = oFile.Width;
                    }
                    int Height = oFile.Height;
                    int Width = oFile.Width;
                    height = width * Height / Width;
                }
                else if (width != 0 && _width < _height)
                {
                    height = width;
                    if (height > oFile.Height)
                    {
                        height = oFile.Height;
                    }
                    int Height = oFile.Height;
                    int Width = oFile.Width;
                    width = height * Width / Height;
                
                }
                else
                {
                    width = oFile.Width;
                    height = oFile.Height;
                }
                Bitmap newBitmap = new Bitmap(oFile.Width, oFile.Height);
                Graphics g = Graphics.FromImage(newBitmap);
                g.DrawImage(oFile, 0, 0, newBitmap.Width, newBitmap.Height);
                Image thumbnailImage = newBitmap.GetThumbnailImage(width, height, callback, IntPtr.Zero);
                ImageSaveAsJPEG.SaveAsJPEG(thumbnailImage, NewFile, 78);
               
                g.Dispose();
                thumbnailImage.Dispose();
                newBitmap.Dispose();
                oFile.Dispose();

               
            }
            catch (Exception ex)
            {
                WebLog.ErrorLog(ex);
            }
            
        }
        protected static bool callback()
        {
            return false;
        }
    }
}
